﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace MedReg.Classes
{
    [Serializable]
    public class StaffEntry : ISerializable
    {


        private long idField;
        
        private Guid LPUId;

        private LPU UZField;

        
        private long BranchId;
        
        private Subdivision BranchField;


        
        private long StuffPostId;
        
        
        private Post StuffPostField;
        
        private decimal QuantityField;
        private string CommentField;


        private static readonly StaffEntry theOneObject = new StaffEntry();
/*
        [OnDeserializing()]
        private void OnDeserializing(StreamingContext context)
        {

            this.Initialize();
        }

        private void Initialize()
        {

            UZField = default(EntityRef<LPU>);
            
        }
*/

        public static StaffEntry GetStaffEntry()
        {
            return theOneObject;
        }
        
        public long Id
        {
            get
            {
                return idField;
            }
            set 
            { 
                idField = value;
            }

        }

        
        public LPU UZ
        {
            get
            {
                return this.UZField;

            }
            set
            {
                this.UZField = value;
            }
        }

        /// <remarks/>
        
        public Subdivision Branch
        {
            get
            {
                return this.BranchField;
                
            }
            set
            {
                this.BranchField = value;
            }
        }

        /// <remarks/>
        
        public Post StuffPost
        {
            get
            {
                return this.StuffPostField;
                
            }
            set
            {
                this.StuffPostField = value;
                
            }
        }
        
        
        public decimal Quantity
        {
            get
            {
                return this.QuantityField;
            }
            set
            {
                this.QuantityField = value;
            }
        }

        /// <remarks/>
        
        public string Comment
        {
            get
            {
                return this.CommentField;
            }
            set
            {
                this.CommentField = value;
            }
        }

        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //throw new NotImplementedException();
            info.AddValue("UZ", UZField);
            info.AddValue("Branch", BranchField);
            info.AddValue("StuffPost", StuffPostField);
            info.AddValue("Quantity", QuantityField);
            info.AddValue("Comment", CommentField);
            
        }

        

        /*
        public StaffEntry (SerializationInfo info, StreamingContext ctxt)
        {
            UZField = (LPU)info.GetValue("UZ", typeof(LPU));
            BranchField = (Subdivision) info.GetValue("Branch", typeof(Subdivision));
            StuffPostField = (Post) info.GetValue("StuffPost", typeof(Post));
            QuantityField = (int) info.GetValue("Quantity", typeof(int));
            CommentField = (string) info.GetValue("Comment", typeof(string));
        }
        
        public StaffEntry (LPU UZ, Subdivision Branch, Post StuffPost, int Quantity, string Comment)
        {
            this.UZField = UZ;
            this.BranchField = Branch;
            this.StuffPostField = StuffPost;
            this.QuantityField = Quantity;
            this.CommentField = Comment;
        }
        */
        public StaffEntry()
        {
//            Initialize();
        }

        /*
         public StaffEntry()
        {
            this.UZField = null;
            this.BranchField = null;
            this.StuffPostField = null;
            this.QuantityField = 0;
            this.CommentField = "";
        }
        */
        public List<Classes.StaffEntry> getStaffEntryList(Classes.ArrayOfStaffEnrty staffEntryCollection)
        {

            var ssSub = staffEntryCollection
                .GroupBy(s => new {s.UZ, s.Branch, s.StuffPost})
                .Select(g => g.First())
                .ToList();
            /*
            var odd = from c in staffEntryCollection
                      group c.UZ by new {c.UZ, c.Branch, c.StuffPost}
                      into g
                      where g.Count() >= 1
                      select g;
            foreach (var i in odd)
            {
                Console.WriteLine(i.Key + " " + i.Count());
            }
            */
            Console.WriteLine(ssSub.Count()==staffEntryCollection.Count());
          return ssSub;
        }
    }
   
}
